/**
 * 预测者网购买的日线数据导入脚本 - 股票数据
 * @type {connect|exports|module.exports}
 */
var mongodb = require('mongodb');
var lineReader = require('line-reader');
var rd = require('rd');

var files = rd.readSync('./stock_data');

files = files.filter(function(item) {
  return /.csv$/.test(item)
})

var server = new mongodb.Server('localhost', 27017, {
  auto_reconnect: true
});

//连接的数据库为stock
var db = new mongodb.Db('stocks', server, {
  safe: true
});

db.open(function(err, db) {
  if (!err) {
    console.log('数据库连接成功')
    console.time('导入全部数据耗时：')

    var totalCount = 0
    var index = 0

    function readAndInsert() {
      var tableName = files[index].match(/(?!\\)[a-zA-Z0-9]+(?=\.csv$)/)[0]

      console.log('开始导入第', index, '个文件,文件名', tableName, '剩余：', files.length - index)

      var collectionName = files[index].match(/(?!\\)[a-zA-Z0-9]+(?=\.csv$)/)[0]

      db.createCollection(collectionName, function(err, collection) {
        if (err) {
          console.log(err);
        } else {

          var lines = []
          var fieldNames = []
          var head = false
          //逐行读取csv文件
          lineReader.eachLine(files[index], function(line, last) {
            if (!head) {
              head = true
              fieldNames = line.split(',')
            } else {
              var fields = line.split(',')

              var json = {}

              fieldNames.forEach(function (item, index) {
                switch (item) {
                  case 'date':
                    json['_id'] = +fields[index].replace(/([0-9]{4})-([0-9]+)-([0-9]+)/, function($0, $1, $2, $3) {
                      return $1+('0'+$2).slice(-2)+('0'+$3).slice(-2)
                    })
                    break
                  case 'code':
                        json['code'] = fields[index]
                        break
                  case 'open':
                  case 'high':
                  case 'low':
                  case 'close':
                  case 'change':
                  case 'volume':
                  case 'money':
                  case 'traded_market_value':
                  case 'market_value':
                  case 'turnover':
                  case 'adjust_price':
                  case 'PE_TTM':
                  case 'PS_TTM':
                  case 'PC_TTM':
                  case 'PB':
                  case 'adjust_price_f':
                    json[item] = +fields[index]
                    break
                  case 'report_type':
                  case 'report_date':
                    json[item] = +fields[index].replace(/([0-9]{4})-([0-9]+)-([0-9]+)/, function($0, $1, $2, $3) {
                      return $1+('0'+$2).slice(-2)+('0'+$3).slice(-2)
                    })
                    break
                  default:
                    json[item] = fields[index]
                    break
                }
              })

              lines.push(json)

              if (last) {
                //存入数据库
                collection.insertMany(lines, {
                  safe: true
                }, function(err, result) {
                  if (err) {
                    console.log(err.message)
                  } else {
                    totalCount += lines.length
                    console.log('导入成功：' + lines.length + ' 条')
                  }

                  index++
                  if (index < files.length) {
                    readAndInsert(index)
                  } else {
                    console.log('数据导入完毕：' + totalCount + ' 条')
                    console.timeEnd('导入全部数据耗时：')
                    db.close()
                    process.exit()
                  }
                });
                return false
              }
            }
          })
        }
      });
    }

    readAndInsert()
  } else {
    console.log(err);
  }
});
